package h0827;

import java.util.*;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 字母异位词分组
 * @date 2024/8/27 17:13
 */
public class GroupAnagrams {
    public static void main(String[] args) {
        String[] s = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> res = getRes(s);
        System.out.println(res);
    }

    private static List<List<String>> getRes(String[] s) {
        if (s == null || s.length == 0) return new ArrayList<>();//边界检查
        Map<String, List<String>> map = new HashMap<>();

        for (int i = 0; i < s.length; i++) {
            String s1 = s[i];
            char[] chars = s1.toCharArray();
            Arrays.sort(chars);
            String temp = new String(chars);
            if (map.containsKey(temp)) {
                List<String> list = map.get(temp);
                list.add(s[i]);

            } else {
                map.put(temp, new ArrayList<>());
                map.get(temp).add(s[i]);
            }
        }
        List<List<String>> res = new ArrayList<>(map.values());
        return res;
    }
}
